package com.zzqstart.cateservice.controller;


import com.zzqstart.cateservice.entity.CateComment;
import com.zzqstart.cateservice.entity.CommentSearch;
import com.zzqstart.cateservice.service.CateCommentService;
import com.zzqstart.commonutils.JwtUtils;
import com.zzqstart.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-06
 */
@RestController
@RequestMapping("/cateservice/comment")
public class CateCommentController {

    @Autowired
    private CateCommentService cateCommentService;

    // 后台评论管理
    @PostMapping("getCommentByCondition/{current}/{limit}")
    public R getCommentByCondition(@PathVariable Long current, @PathVariable Long limit,@RequestBody(required = false) CommentSearch searchObj) {
        Map<String, Object> map = cateCommentService.getCommentByCondition(searchObj, current, limit);
        return R.ok().data(map);
    }

    // 查询菜谱评论
    @GetMapping("getCommentByRecipeId/{recipeId}/{current}/{limit}")
    public R getCommentByRecipeId(@PathVariable String recipeId, @PathVariable Long current, @PathVariable Long limit) {
        Map<String, Object> map = cateCommentService.getCommentByRecipeId(recipeId, current, limit);
        return R.ok().data("map", map);
    }

    // 发送评论
    @PostMapping("/sendComment")
    public R sendComment(@RequestBody CateComment cateComment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isSend = cateCommentService.sendComment(cateComment, memberId);

        if (isSend) {
            return R.ok();
        } else {
            return R.error().message("发送失败");
        }
    }

    // 删除评论
    @DeleteMapping("/deletedComment/{commentId}")
    public R deletedComment(@PathVariable String commentId,HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isDel = cateCommentService.deletedComment(memberId,commentId);
        if (isDel){
            return R.ok();
        }else {
            return R.error().message("删除失败");

        }
    }
}

